package com.bella.api.configuration;

import com.bella.api.utils.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 李杰 E-mail:605779485@qq.com
 * @version 1.0.0 创建时间：2019年4月1日
 */
@Configuration
@ConditionalOnClass(RedisClient.class)
public class JedisConfig {
    private Logger logger = LoggerFactory.getLogger(JedisConfig.class);

    //   @Value("${redis.host}")
    private String host = "localhost";
    //    @Value("${redis.port}")
    private int port = 6379;
    //    @Value("${redis.password}")
    private String password = "zsq950316";

    @Bean(name = "jedisPool")
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(2);
        config.setMaxIdle(2);
        config.setMaxWaitMillis(1000);
        return new JedisPool(config, host, port, 3000, password);
    }

    @Bean
    @ConditionalOnMissingBean(RedisClient.class)
    public RedisClient redisClient(@Qualifier("jedisPool") JedisPool pool) {
        logger.info("初始化……Redis Client==Host={},Port={}", host, port);
        RedisClient redisClient = new RedisClient();
        redisClient.setJedisPool(pool);
        return redisClient;
    }
}
